package com.woniuxy.provider;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

import static org.apache.ibatis.jdbc.SelectBuilder.SELECT;

public class EmpProvider {
    //内部类如果要访问外部类的局部变量，需要在外部类局部变量前加入final关键字做修饰
    public String select(final Map<String,Object> params){
      return new SQL(){
          {
            SELECT("*");
            FROM("emp");
            StringBuilder whereCaluse=new StringBuilder();
            if(params.get("ename")!=null&&!"".equals(params.get("ename"))){
                whereCaluse.append("AND").append("ename LIKE CONCAT('%',#{ename},'%')");
            }
            if(params.get("job")!=null&&!"".equals(params.get("job"))){
                whereCaluse.append(" AND").append(" job=#{job}");
            }
            if(params.get("ename")!=null||params.get("job")!=null){
                WHERE(whereCaluse.toString().replaceFirst("AND",""));
            }
          }
      }.toString();
    }
}
